﻿#region << 版 本 注 释 >>
/*
     * ========================================================================
     * Copyright Notice  2010-2021 TideBuy.com All rights reserved .
     * ========================================================================
     * 机器名称：DESKTOP-VJFB8FV 
     * 文件名：  DelegateCommand 
     * 版本号：  V1.0.0.0 
     * 创建人：  徐一贺 
     * 创建时间： 2021/3/6 14:13:29 
     * 描述    :
     * =====================================================================
     * 修改时间：2021/3/6 14:13:29 
     * 修改人  ： 徐一贺
     * 版本号  ： V1.0.0.0 
     * 描述    ：
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CoilIntelStorage.Commands
{
    public class DelegateCommand : ICommand
    {
        private Action<object> executeAction;
        private Func<object, bool> canExecuteFunc;
        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<object> execute)
            : this(execute, null)
        { }

        public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
        {
            if (execute == null)
            {
                return;
            }
            executeAction = execute;
            canExecuteFunc = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (canExecuteFunc == null)
            {
                return true;
            }
            return canExecuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if (executeAction == null)
            {
                return;
            }
            executeAction(parameter);
        }
    }

    // DelegateCommand的泛型版本
    public class DelegateCommand<T> : ICommand
    {
        private Action<T> executeAction;
        private Func<T, bool> canExecuteFunc;
        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<T> execute)
            : this(execute, null)
        { }

        public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            if (execute == null)
            {
                return;
            }
            executeAction = execute;
            canExecuteFunc = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (canExecuteFunc == null)
            {
                return true;
            }
            return canExecuteFunc((T)parameter);
        }

        public void Execute(object parameter)
        {
            if (executeAction == null)
            {
                return;
            }
            executeAction((T)parameter);
        }
    }
}
